/**
 * 
 */
package com.cmcc.xdr.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Date 2018年6月12日 下午4:29:53
 * @author Amish
 */
public class PacketAnalysisConfig {
	private static Logger logger = LogManager.getLogger(PacketAnalysisConfig.class);
	static Properties p ;

	private static PacketAnalysisConfig instance = new PacketAnalysisConfig();
	public static PacketAnalysisConfig getInstance() {
		return instance;
	}
	
	private PacketAnalysisConfig() {
		this.parseFile();
	}
	
	
    private void parseFile(){
//    	String path = PacketAnalysisConfig.class.getResource("/").getPath()+"config/packetanalysis.properties";
//    	path = path.replace("%20", " ");
//    	path = path.replace("%23", "#");
//    	//File file = new File(path);
//    	InputStream in;
		try {
			InputStream in = PacketAnalysisConfig.class.getClassLoader().getResourceAsStream("config/packetanalysis.properties");
//			in = new BufferedInputStream(new FileInputStream(path));
			p = new Properties();
			p.load(in);
		} catch (FileNotFoundException e) {
			logger.error(e.getCause(), e);
		} catch (IOException e) {
			logger.error(e.getCause(), e);
		}
    }
    
    public static String getConfigAsString(String key) {
    	return p.getProperty(key).trim();
    }
    
    public static long getConfigAsLong(String key) {
    	String value = p.getProperty(key).trim();
    	if(value != null) {
    		return Integer.valueOf(value).longValue();
    	}else {
    		return 0;
    	}
    }
    
    public static int getConfigAsInt(String key) {
    	String value = p.getProperty(key).trim();
    	if(value != null) {
    		return Integer.valueOf(value).intValue();
    	}else {
    		return 0;
    	}
    }
    
    //only for test
    public static void main(String[] args) {
    	System.out.println(PacketAnalysisConfig.getConfigAsString("tcp.connection.overtime"));
    	System.out.println(PacketAnalysisConfig.getConfigAsLong("tcp.connection.overtime"));
    	System.out.println(PacketAnalysisConfig.getConfigAsString("tcp.packet.dl.threshold"));
    	System.out.println(PacketAnalysisConfig.getConfigAsInt("tcp.packet.dl.threshold"));
    	System.out.println(PacketAnalysisConfig.getConfigAsLong("tcp.connection.delaytime"));
    	
    	System.out.println(PacketAnalysisConfig.getConfigAsLong("tcp.connection.s"));
	}

}
